package com.jmpt.fangbohui.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.BrokerShopBean;
import com.jmpt.fangbohui.bean.OldHouseBean;
import com.jmpt.fangbohui.bean.BrokerShopBean.Hot_eshouseEntity;
import com.jmpt.fangbohui.ui.BrokerHouseListActivity;
import com.jmpt.fangbohui.ui.BuyOldHouseListActivity;
import com.jmpt.fangbohui.ui.NewHouseDetailActivity;
import com.jmpt.fangbohui.ui.OldHouseDetailActivity;
import com.jmpt.fangbohui.utils.ImageLoaderHelper;
import com.jmpt.fangbohui.utils.UIUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @包名:  com.jmpt.fangbohui.ui.fragment
 * @类名:  BrokerShopHouseListFragment
 * @作者:  China_Victor 724932439#qq.com
 * @创建时间 : 2015-9-1 下午4:21:35
 *
 * @描述: 经纪人网店 二手房源 列表
 */
public class BrokerShopOldHouseListFragment extends Fragment {
	
	public List<Hot_eshouseEntity> houseList = new ArrayList<Hot_eshouseEntity>();
	
	private ListView mListView;

	private String uid;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return getContentView();
	}

	private ListView getContentView() {
		if (mListView == null) {
			mListView = new ListView(UIUtils.getContext());
			mListView.setCacheColorHint(Color.TRANSPARENT);
			mListView.setSelector(android.R.color.transparent);
			mListView.setFadingEdgeLength(0);
			mListView.setDividerHeight(1);
			//mListView.setDivider(getResources().getDrawable(R.drawable.liner));
			mListView.setBackgroundColor(UIUtils.getColor(R.color.white));
			mListView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
			mListView.setId(R.id.id_stickynavlayout_innerscrollview);
		}else{
			ViewGroup parent = (ViewGroup) mListView.getParent();
            if (parent != null) {
                parent.removeView(mListView);
            }
		}
		
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView,
					View view, int i, long l) {
			     try {
			    	//显示房屋详情
					 Intent intent = new Intent(getActivity(), OldHouseDetailActivity.class);
					 intent.putExtra(OldHouseDetailActivity.HOUSE_ID, Integer.parseInt(houseList.get(i).HouseID));
					 intent.putExtra(OldHouseDetailActivity.HOUSE_TITLE, houseList.get(i).H_Title);
					 startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		return mListView;
	}
	
	public void setData(List<Hot_eshouseEntity> hot_eshouse, String uid) {
		this.houseList = hot_eshouse;
		this.uid = uid;
		getContentView().setAdapter(new OldHouseListAdapter());
		getContentView().addFooterView(getMoreDataView());
	}

	private View getMoreDataView() {

		View view = View.inflate(UIUtils.getContext(),
				R.layout.foot_more_data_view, null);
		Button more = (Button) view.findViewById(R.id.btn_more);
		more.setText("更多二手房信息");
		more.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getActivity(),
						BrokerHouseListActivity.class);
				intent.putExtra(BrokerHouseListActivity.SALE_OR_RENT, BrokerHouseListActivity.SALE);
				intent.putExtra(BrokerHouseListActivity.UID, uid);
				startActivity(intent);
			}
		});

		return view;
	}
	
	public class OldHouseListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return houseList.size();
		}

		@Override
		public Object getItem(int position) {
			return houseList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO 填充旧房条目
			if (convertView == null) {
				convertView = View.inflate(UIUtils.getContext(), R.layout.item_list_broker_oldhouse,
						null);
			}
			Holder holder = getHolder(convertView);
			Hot_eshouseEntity bean = houseList.get(position);
			ImageLoaderHelper.displayImage(bean.picUrl, holder.housePhoto);
			holder.houseTitle.setText(bean.H_Title);
			holder.houseRoomAndHall.setText(bean.H_Room + "房" + bean.H_Hall + "厅");
			holder.houseProportion.setText(bean.H_MJ + "平米");
			holder.houseTotalPrice.setText(String.valueOf(bean.H_price + bean.price_type));
			holder.houseAddress.setText("");
			holder.age.setText(TextUtils.isEmpty(bean.H_Age) ? "" : "楼龄：" + bean.H_Age);
			if (!TextUtils.isEmpty(bean.ZXName)) {
				holder.houseFitment.setVisibility(View.VISIBLE);
				holder.houseFitment.setText(bean.ZXName);
			} else {
				holder.houseFitment.setVisibility(View.INVISIBLE);
			}
			return convertView;

		}

		private Holder getHolder(final View view) {
			Holder holder = (Holder) view.getTag();
			if (holder == null) {
				holder = new Holder(view);
				view.setTag(holder);
			}
			return holder;
		}

		private class Holder {
			public ImageView housePhoto;
			public TextView houseTitle;
			public TextView houseRoomAndHall;
			public TextView houseProportion;
			public TextView houseTotalPrice;
			public TextView houseAddress;
			public TextView houseFitment; // 装修
			public TextView age; // 装修

			public Holder(View view) {
				housePhoto = (ImageView) view
						.findViewById(R.id.iv_oldhouse_item_photo);
				houseTitle = (TextView) view
						.findViewById(R.id.tv_oldhouse_item_title);
				houseRoomAndHall = (TextView) view
						.findViewById(R.id.tv_oldhouse_item_roomAndHall);
				houseProportion = (TextView) view
						.findViewById(R.id.tv_oldhouse_item_proportion);
				houseTotalPrice = (TextView) view
						.findViewById(R.id.tv_oldhouse_item_totalPrice);
				houseAddress = (TextView) view
						.findViewById(R.id.tv_oldhouse_item_addr);
				houseFitment = (TextView) view
						.findViewById(R.id.tv_oldhouse_item_fitment);
				age = (TextView) view
						.findViewById(R.id.tv_oldhouse_item_age);
			}
		}
	}

}
